﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WpfLibrary;

public class TabControlDragable : TabControl
{
    public TabControlDragable()
    {
        AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseLeftButtonDown), true);
        AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseLeftButtonUp), true);
        AddHandler(MouseMoveEvent, new MouseEventHandler(OnMouseMove), true);

        AddHandler(DropEvent, new DragEventHandler(OnDrop), true);
        MinWidth = 100;
        MinHeight = 100;
        AllowDrop = true;
    }
    private bool IsDraging = false;
    private object? DragData;
    private Point DragStartPosition;

    private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Point p = e.GetPosition(this);
        DragData = DragUtility.GetDataObjectFromItemsControl(this, p);
        if (DragData != null)
        {
            DragStartPosition = p;
        }
        e.Handled = true;
    }
    private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        ResetState();
        e.Handled = true;
    }
    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (DragData == null || DragData is not TabItem)
        {
            return;
        }
        Point currentPosition = e.GetPosition(this);
        if (e.LeftButton == MouseButtonState.Pressed && !IsDraging
            && ((Math.Abs(currentPosition.X - DragStartPosition.X) > SystemParameters.MinimumHorizontalDragDistance) || (Math.Abs(currentPosition.Y - DragStartPosition.Y) > SystemParameters.MinimumVerticalDragDistance)))
        {
            DataObject dataObject = new DataObject();
            dataObject.SetData("DragData", DragData);
            var effects = DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Move);
            if (effects == DragDropEffects.None)
            {
                if (Items.Count != 1)
                {
                    var tabItem = DragData as TabItem;
                    var parentWindow = DragUtility.GetParentWindow(tabItem);
                    if (parentWindow is ChildWindow)
                    {
                        parentWindow = parentWindow.Parent as Window;
                    }
                    Items.Remove(tabItem);
                    ChildWindow newChildWindow = new ChildWindow();
                    newChildWindow.Owner = parentWindow;
                    newChildWindow.DataContext = parentWindow?.DataContext;
                    newChildWindow.Title = parentWindow?.Title + " - ChildWindow";
                    var tabControl = new TabControlDragable();
                    newChildWindow.MyGrid.Children.Add(tabControl);
                    tabControl.Items.Add(tabItem);
                    newChildWindow.Show();
                }
            }
            ResetState();
        }
        e.Handled = true;
    }
    private int MeasureIndex(TabControl targetTabControl, Point point)
    {
        double preWidth = 0.0;
        for (var i = 0; i < targetTabControl.Items.Count; i++)
        {
            var item = targetTabControl.Items[i] as TabItem;
            if (item == null)
            {
                continue;
            }
            if (point.Y > item.ActualHeight)
            {
                return targetTabControl.Items.Count;
            }
            if (point.X < preWidth + item.ActualWidth / 2)
            {
                return i;
            }
            else
            {
                preWidth += item.ActualWidth;
            }
        }
        return targetTabControl.Items.Count;
    }
    private void OnDrop(object sender, DragEventArgs e)
    {
        var tabItem = e.Data.GetData("DragData") as TabItem;
        var parentTabControl = tabItem?.Parent as TabControl;
        if (tabItem == null || parentTabControl == null)
        {
            return;
        }
        var point = e.GetPosition(this);
        int index = MeasureIndex(this, point);
        if (this != parentTabControl)
        {
            var parentWindow = DragUtility.GetParentWindow(parentTabControl);
            if (parentTabControl.Items.Count == 1)
            {
                var childWindow = parentWindow as ChildWindow;
                if (childWindow != null)
                {
                    parentTabControl.Items.Remove(tabItem);
                    this.Items.Insert(index, tabItem);
                    this.SelectedItem = tabItem;
                    childWindow?.Close();
                }
            }
            else
            {
                parentTabControl.Items.Remove(tabItem);
                this.Items.Insert(index, tabItem);
                this.SelectedItem = tabItem;
            }
        }
        else if (index < parentTabControl.Items.IndexOf(tabItem))
        {
            parentTabControl.Items.Remove(tabItem);
            parentTabControl.Items.Insert(index, tabItem);
            parentTabControl.SelectedItem = tabItem;
        }
        else if (index > parentTabControl.Items.IndexOf(tabItem))
        {
            parentTabControl.Items.Remove(tabItem);
            parentTabControl.Items.Insert(index - 1, tabItem);
            parentTabControl.SelectedItem = tabItem;
        }
        e.Handled = true;
    }

    private void ResetState()
    {
        IsDraging = false;
        DragData = null;
    }
}
